import React, { Component } from 'react'

export default class App extends Component {
  constructor(props) {
    super(props)
  
    this.state = {
       majors:['se','cs','mis','iot'],
       major:''
    }
  }
  _del=(major)=>{
    let index=this.state.majors.indexOf(major)
    let majors=this.state.majors.slice(0)
    majors.splice(index,1)
    this.setState({majors})
  }
  _onChange=event=>{
      this.setState({major:event.target.value})
  }
  _add=()=>{
      let majors=[...this.state.majors]
      majors.push(this.state.major)
      this.setState({majors})
  }
  
  render() {
    return (
      <div>
        <input type="text" value={this.state.major} 
        onChange={this._onChange}/>
        <button onClick={this._add}>添加</button>
        {this.state.majors.map(major=><li>{major}
        <button onClick={()=>this._del(major)}> 删除</button>
        </li>)}
      </div>
    )
  }
}
